package com.stone.im.sdk.android;

import com.stone.im.common.Constants;
import com.stone.im.common.Message;
import com.stone.im.common.Request;
import com.stone.im.common.Response;
import com.stone.im.protobuf.proto.AuthenticateResponseProto;
import com.stone.im.protobuf.proto.MessagePushRequestProto;
import com.stone.im.protobuf.proto.MessagePushResponseProto;
import com.stone.im.protobuf.proto.MessageSendResponseProto;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.SocketChannel;

public class ImClientHandler extends ChannelInboundHandlerAdapter {

    private ImClient imClient;

    public ImClientHandler(ImClient imClient){
        this.imClient=imClient;
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        //建立完毕
        //在这里发送一个token过去，尝试验证

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        SocketChannel channel = (SocketChannel)ctx.channel();

        Message message = new Message((ByteBuf) msg);
            if (message.getMessageType() == Constants.MESSAGE_TYPE_RESPONSE){

                Response response = message.toResponse();
                if (response.getRequestType() == Constants.REQUEST_TYPE_AUTHENTICATE){
                    AuthenticateResponseProto.AuthenticateResponse authenticateResponse = AuthenticateResponseProto.AuthenticateResponse.parseFrom(response.getBody());
                    System.out.println("认证请求收到响应"+ authenticateResponse);
                }else if (response.getRequestType() == Constants.REQUEST_TYPE_SEND_MESSAGE){
                    MessageSendResponseProto.MessageSendResponse messageSendResponse = MessageSendResponseProto.MessageSendResponse.parseFrom(response.getBody());
                    System.out.println("客户端收到发送单聊消息的响应："+messageSendResponse);
                }
            }else if (message.getMessageType() == Constants.MESSAGE_TYPE_REQUEST){
                Request request = message.toRequest();

                if (request.getRequestType() == Constants.REQUEST_TYPE_PUSH_MESSAGE){
                    MessagePushRequestProto.MessagePushRequest messagePushRequest = MessagePushRequestProto.MessagePushRequest.parseFrom(request.getBody());
                    System.out.println("接收到一条消息推送" + messagePushRequest);

                    MessagePushResponseProto.MessagePushResponse.Builder builder = MessagePushResponseProto.MessagePushResponse.newBuilder();
                    builder.setMessageId(messagePushRequest.getMessageId());

                    MessagePushResponseProto.MessagePushResponse messagePushResponse = builder.build();
                    Response response = new Response(request,messagePushResponse.toByteArray());

                    channel.writeAndFlush(response.getBuffer());
                }
            }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
